<template>
  <div class="_wz_cent_main _wz_DailyTrai">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>

      <Form :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="培训课程">
          <i-input v-model="formDataSearch.planName" placeholder="培训课程" clearable />
        </FormItem>
        <FormItem label="培训方向">
          <i-select v-model="formDataSearch.planDirection" filterable placeholder="培训方向" clearable>
            <Option v-for="item in optionsData" :value="item.code" :key="item.code">{{ item.value }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="培训进度" v-if="searchType">
          <i-select v-model="formDataSearch.finishState" filterable placeholder="培训进度" clearable>
            <Option value="0">未开始</Option>
            <Option value="2">进行中</Option>
            <Option value="1">已完成</Option>
          </i-select>
        </FormItem>
        <FormItem label="培训周期" v-if="searchType">
          <DatePicker type="month" v-model="formDataSearch.wzYearMonth" placeholder="请选择年月"></DatePicker>
        </FormItem>
        <FormItem label="发布状态" v-if="searchType">
          <i-select v-model="formDataSearch.releaseState" filterable placeholder="发布状态" clearable>
            <Option value="1">已发布</Option>
            <Option value="0">未发布</Option>
            <Option value="2">发布中</Option>
            <Option value="-1">发布失败</Option>
          </i-select>
        </FormItem>
        <FormItem label="随堂测验" v-if="searchType">
          <i-select v-model="formDataSearch.examState" filterable placeholder="随堂测验" clearable>
            <Option value="1">存在</Option>
            <Option value="0">未存在</Option>
          </i-select>
        </FormItem>
        <FormItem label="运输企业" v-if="searchType">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>

      </Form>
    </Card>

    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addTableData">添加</Button>
        <Button type="success" @click="ExportData">导出</Button>
      </p>

      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>

    <Drawer title="查看素材" v-model="materialType" width="1000" :draggable="true" :styles="offlineModelStyles">
      <materialList v-if="materialType" :materialList="materialList"></materialList>
    </Drawer>

    <Drawer title="教育培训" v-model="formOperation" width="800" :draggable="true" :mask-closable="false" :styles="formOperationStyles">
      <courseAdd v-if="formOperation" :selectData="selectData" @closeFun="closeFun"></courseAdd>
    </Drawer>

    <Drawer title="培训列表" v-model="trainListType" width="1200" :draggable="true" :mask-closable="false">
      <trainList v-if="trainListType" :selectData="selectData"></trainList>
    </Drawer>
    <Spin size="large" fix v-if="spinShow"></Spin>


  </div>
</template>
<script>
  import courseAdd from './courseAdd.vue'
  import materialList from '../public/materialList.vue'
  import wzTbale from "../../../components/table/table";
  import trainList from './trainList.vue'


  export default {
    components: {
      courseAdd,//培训课题添加
      materialList,//查看素材
      wzTbale,//列表
      trainList,//培训列表
    },
    data() {
      return {
        spinShow:false,//本页加载
        searchType:false,//搜索条件
        trainListType:false,
        subDeptList: [],//运输企业
        materialList: [],//素材列表 查看素材使用
        materialType: false,//查看素材弹出
        optionsData: [],//培训方向数据
        tableLogging: true,//列表加载
        selectData: {},
        formDataSearch: {},//搜索表单数据

        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        offlineModelStyles: {//表单左侧移出css
          height: 'calc(100% - 51px)',
          overflow: 'auto',
          padding: '10px',
          position: 'static'
        },
        tableData: {
          url: this.apiUrl.educateTrainPlanTrainCoursePlanPageList,//请求地址
          method: 'post',//请求方式
          where: {},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表头数据
            {type: 'index', width: 70, align: 'center', title: '序号'},
            {
              title: '培训周期', minWidth: 120, render: (h, params) => {
                return h('span', params.row.trainCoursePlanCreateYear + '年' + params.row.trainCoursePlanCreateMonth + '月');
              }
            },
            {title: '培训课程', minWidth: 200, key: 'trainCoursePlanName'},
            {title: '培训类型', minWidth: 110, key: 'trainCoursePlanTypeName'},
            {title: '培训方向', minWidth: 100, key: 'trainCoursePlanDirectionName'},
            {
              title: '发布状态', minWidth: 140, render: (h, params) => {
                //  0：未开始，1：已完成，-1：进行中
                if (params.row.trainCoursePlanRelease == 1) {
                  return h('Tag', {props: {type: 'dot',color: 'success'}}, '已发布')
                } else if (params.row.trainCoursePlanRelease == 2) {
                  return h('Tag', {props: {type: 'dot',color: 'primary'}}, '发布中')
                } else if (params.row.trainCoursePlanRelease == 0) {
                  return h('Tag', {props: {type: 'dot',color: 'warning'}}, '未发布')
                } else if (params.row.trainCoursePlanRelease == -1) {
                  return h('Tag', {props: {type: 'dot',color: 'error'}}, '发布失败')
                }
              }
            },
            {
              title: '随堂测验', minWidth: 140, render: (h, params) => {
                //  0：未开始，1：已完成，-1：进行中
                if (params.row.trainCoursePlanExam == 1) {
                  return h('Tag', {props: {type: 'dot',color: 'success'}}, '已添加')
                } else {
                  return h('Tag', {props: {type: 'dot',color: 'default'}}, '未添加')
                }
              }
            },
            {
              title: '培训人数', minWidth: 100, render: (h, params) => {
                return h('span', params.row.trainCoursePlanFinishedNumber + '/'+params.row.trainCoursePlanTotalNumber + '人');
              }
            },
            {
              title: '培训时长', minWidth: 100, render: (h, params) => {
                return h('span', params.row.trainCoursePlanTotalDuration + '分钟');
              }
            },
            {title: '开始时间', minWidth: 100, key: 'trainCoursePlanBeginDate'},
            {title: '结束时间', minWidth: 100, key: 'trainCoursePlanEndDate'},
            {title: '运输企业', minWidth: 260, key: 'deptName'},
            {
              title: '操作', fixed: 'right', width: 120,
              render: (h, params) => {
                let operationButtons = [];
                //列表  发布后 可以查看
                if (params.row.trainCoursePlanRelease == 1) {
                  operationButtons.push(h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.trainListType = true;
                      }
                    }
                  }, '列表'))
                }

                //发布
                if (params.row.trainCoursePlanRelease == 0) {
                  if(operationButtons.length > 0){
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        let that = this;
                        that.$Modal.confirm({
                          title: '发布提醒',
                          content: '确定要发布选中的课程吗?已经发布,不可以修改删除操作',
                          okText: '发布',
                          cancelText: '取消',
                          onOk: () => {
                            that.spinShow = true;
                            that.axios.post(that.apiUrl.educateTrainPlanTrainCoursePlanRelease, {uuid: params.row.uuid}).then(res => {
                              that.spinShow = false;
                              if (!!res) {
                                that.$Message.success('发布成功');
                                that.$refs.wzTbale.tableLoadData();
                              }
                            }).catch(err => {
                              console.log('失败', err);
                            })
                          },
                          onCancel: () => {
                            // //取消 修改课程
                            // that.selectData = JSON.parse(JSON.stringify(params.row));
                            // that.formOperation = true;
                          }
                        });
                      }
                    }
                  }, '发布'))
                }
                var buttonList = [];



                //进度刷新
                if (params.row.trainCoursePlanRelease == 1) {
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        let that = this;
                        that.$Modal.confirm({
                          title: '进度刷新提醒',
                          content: '确定要进度刷新选中的数据？',
                          onOk: () => {
                            that.spinShow = true;
                            that.axios.post(that.apiUrl.educateTrainPlanTrainCoursePlanRefresh, {uuid: params.row.uuid}).then(res => {
                              this.spinShow = false;
                              if (!!res) {
                                that.$Message.success('进度刷新成功');
                                that.$refs.wzTbale.tableLoadData();
                              }
                            }).catch(err => {
                              console.log('失败', err);
                            })
                          },
                          onCancel: () => {
                            //点击取消
                          }
                        });
                      }
                    }
                  }, '进度刷新'))
                }


                // if(params.row.trainCoursePlanRelease != 0){
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.seeMaterial(params.row);
                      }
                    }
                  }, '课件'))
                // }

                //修改
                if (params.row.trainCoursePlanRelease != 1) {
                  buttonList.push(h('DropdownItem', {
                    nativeOn: {
                      click: () => {
                        this.selectData = JSON.parse(JSON.stringify(params.row));
                        this.formOperation = true;
                      }
                    }
                  }, '修改'))
                }
                //删除
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      let that = this;
                      that.$Modal.confirm({
                        title: '删除提醒',
                        content: '确定要删除选中的课程？',
                        onOk: () => {
                          that.spinShow = true;
                          that.axios.post(that.apiUrl.educateTrainPlanTrainCoursePlanDelete, {uuid: params.row.uuid}).then(res => {
                            that.spinShow = false;
                            if (!!res) {
                              that.$Message.success('删除成功');
                              that.$refs.wzTbale.tableLoadData();
                            }
                          }).catch(err => {
                            console.log('失败', err);
                          })
                        },
                        onCancel: () => {
                          //点击取消
                        }
                      });
                    }
                  }
                }, '删除'))

                if (buttonList.length > 0) {
                  if(operationButtons.length > 0){
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                    [
                      h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                      h('DropdownMenu', {slot: "list"}, buttonList)
                    ])
                  )
                }
                return operationButtons;
              }
            }
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },


      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态

    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      let that = this;

      //运输企业
      that.axios.post(that.apiUrl.coreDeptCoreDeptList, {}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })

      //加载方向
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: 'training_direction'}).then(res => {
        if (!!res) {
          that.optionsData = res.data.data
        }
      }).catch(err => {
        console.log('失败', err);
      })
    },
    methods: {//执行的方法
      addTableData() {//table添加
        this.selectData = {};
        this.formOperation = true;
      },
      searchSubmit() {//搜索
        this.formDataSearch.createYear = !!this.formDataSearch.wzYearMonth? this.public.timeFormatting(this.formDataSearch.wzYearMonth, "yyyy"):'';
        this.formDataSearch.createMonth = !!this.formDataSearch.wzYearMonth? this.public.timeFormatting(this.formDataSearch.wzYearMonth, "MM"):'';
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      //关闭 弹出框刷新列表
      closeFun(data) {
        if (data) {//添加修改 刷新表单
          this.$refs.wzTbale.tableLoadData();
        }
        this.formOperation = false;
      },
      //查看课件
      seeMaterial(data) {
        let that = this;
        that.materialList = [];
        that.spinShow = true;
        that.axios.post(that.apiUrl.educateMaterialTrainMaterialTrainList, {uuids: data.trainCoursePlanMaterialUuids}).then(res => {
          if (!!res) {
            res.data.data.forEach(function (k, i) {
              let data = {};
              data.name = k.materialTrainName;//名称
              data.typeName = k.materialTrainDirectionName;//类型
              data.time = k.trainCoursePlanTaskDuration;//时长
              if (k.materialTrainType == 1) {//视频
                data.type = 1;
                data.videoSdUrl = k.materialTrainVideoSdUrl;//标清：SD
                if (!!k.materialTrainVideoHdUrl) {//高清：HD
                  data.videoHdUrl = k.materialTrainVideoHdUrl;
                }
                if (!!k.materialTrainVideoFdUrl) {//超清：FD
                  data.videoFdUrl = k.materialTrainVideoFdUrl;
                }
              } else {
                data.type = 2;
                data.htmlUrl = k.materialTrainHtmlUrl;//文件路径
                data.mp3Url = k.materialTrainMp3Url;//mp3路径
              }
              that.materialList.push(data);
            })
            if (!!data.materialExamUuid) {

              that.materialList.push({
                name: data.materialExamName,
                type: 3,
                id: data.materialExamUuid,
                typeName: data.trainCoursePlanDirectionName,
              });

              that.materialType = true;
              that.spinShow = false;
            } else {
              that.materialType = true;
              that.spinShow = false;
            }

          }
        }).catch(err => {
          console.log('失败', err);
        })
      },
      //导出
      ExportData(){
        var that = this;
        that.spinShow = true;
        that.axios.post(that.apiUrl.educateTrainPlanTrainCoursePlanExport,this.formDataSearch).then(res => {
          that.spinShow = false;
          if (!!res) {
            window.open(that.apiUrl.imgUrl + '/' + res.data.data)
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_DailyTrai {
    height: 100%;
    display: flex;
    flex-direction: column;
  }
</style>
